package com.maya.medicalwastes.util;

import android.util.Log;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by GZL on 2015/7/17.
 * <p>
 * Description Date 工具类
 * <p>
 * version 1.0
 */
public class DateUtils {
    public final static String DATE_SEC_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public final static String DATE_SEC_FORMAT_NO_SECOND = "yyyy-MM-dd HH:mm";
    public final static String TIME_FORMAT_SEPERATE = "yyyy-MM-dd";
    public final static String CN_TIME_FORMAT_SEPERATE = "yyyy年MM月dd日 HH:mm:ss";
    public final static String HOUR_OF_MINUTE = "HH:mm";
    public final static String MONTH_OF_DAY = "MM月dd日";
    public final static String THIS_APP_TIME_FORMAT = "MM.dd HH:mm";
    public final static String TIME_FORMAT = "MM月dd日 HH:mm";
    public final static String BILL_TIME = "yyyy.MM.dd";
    public final static String BUSINESS_ORDER_TIME = "yy MM dd"; //客户交易详情时间 example: 15 12 2
    public final static String DATE_HOURS = "MM-dd HH:mm";

    /**
     * @param startTime
     * @param endTimeStr
     * @return Map<String, Integer> key1: diffHours; key2: diffMinutes
     */
    public static Map<String, Integer> getDifferentHoursAndMinutes(Date startTime, String endTimeStr) {
        Map<String, Integer> resultMap = new HashMap<String, Integer>();
        Date endTime = null;
        if (startTime == null) {
            resultMap.put("diffHours", 0);
            resultMap.put("diffMinutes", 0);
            resultMap.put("diffSeconds", 0);
            return resultMap;
        }
        try {
            String today = getTimeByFromat(startTime, TIME_FORMAT_SEPERATE);
            if (endTimeStr == null || endTimeStr == "") {
                endTimeStr = today + " 23:59:59";
            } else {
                endTimeStr = today + " " + endTimeStr;
            }
            Log.i("startTime:" + startTime + ", endTimeStr:", endTimeStr);
            endTime = parseDateStr(endTimeStr, DATE_SEC_FORMAT);
        } catch (Exception e) {
            Log.e("日期解析出错", e.getMessage());
        }
        long hoursDiff = diff(startTime, endTime, Calendar.HOUR);
        long minutesDiff = diff(startTime, endTime, Calendar.MINUTE);
        long diffSeconds = diff(startTime, endTime, Calendar.SECOND);
        minutesDiff = minutesDiff - hoursDiff * 60;
        diffSeconds = diffSeconds - (hoursDiff * 60 * 60);
        resultMap.put("diffHours", Integer.parseInt(hoursDiff + ""));
        resultMap.put("diffMinutes", Integer.parseInt(minutesDiff + ""));
        resultMap.put("diffSeconds", Integer.parseInt(diffSeconds + ""));

        return resultMap;
    }

    /**
     * @param startTime
     * @param endTime
     * @return Map<String, Integer> key1: diffHours; key2: diffMinutes;key3: diffSeconds
     */
    public static Map<String, Integer> getDifferentHoursAndMinutes(Date startTime, Date endTime) {
        Map<String, Integer> resultMap = new HashMap<String, Integer>();
        if (startTime == null) {
            resultMap.put("diffHours", 0);
            resultMap.put("diffMinutes", 0);
            resultMap.put("diffSeconds", 0);
            return resultMap;
        }
        long hoursDiff = diff(startTime, endTime, Calendar.HOUR);
        long minutesDiff = diff(startTime, endTime, Calendar.MINUTE);
        long diffSeconds = diff(startTime, endTime, Calendar.SECOND);
        minutesDiff = minutesDiff - hoursDiff * 60;
        //diffSeconds = diffSeconds - (hoursDiff * 60 * 60);
        diffSeconds = (diffSeconds % 3600) % 60;
        resultMap.put("diffHours", Integer.parseInt(hoursDiff + ""));
        resultMap.put("diffMinutes", Integer.parseInt(minutesDiff + ""));
        resultMap.put("diffSeconds", Integer.parseInt(diffSeconds + ""));

        return resultMap;
    }

    /**
     * Calculate the difference of specified field between 2 dates.
     * e.g. begin: 2012-02-01 10:00:00,
     * end: 2012-02-02 11:00:00
     * <p>
     * If field is Calendar.DAY, then the result is 1.
     * If field is Calendar.HOUR, then the result is 25.
     *
     * @param begin
     * @param end
     * @param field
     * @return
     * @see Calendar
     */
    public static long diff(Date begin, Date end, int field) {
        long diff = end.getTime() - begin.getTime();
        if (Calendar.MILLISECOND == field) {
            return diff;
        }
        diff /= 1000;
        if (Calendar.SECOND == field) {
            return diff;
        }
        diff /= 60;
        if (Calendar.MINUTE == field) {
            return diff;
        }
        diff /= 60;
        if (Calendar.HOUR == field || Calendar.HOUR_OF_DAY == field) {
            return diff;
        }
        diff /= 24;
        if (Calendar.DAY_OF_MONTH == field || Calendar.DAY_OF_WEEK == field
                || Calendar.DAY_OF_WEEK_IN_MONTH == field || Calendar.DAY_OF_YEAR == field) {
            return diff;
        }
        if (Calendar.MONTH == field) {
            return diff / 30;
        }
        if (Calendar.YEAR == field) {
            return diff / 365;
        }

        return diff;
    }

    /**
     * parse date string to Date object.
     *
     * @param dateStr
     * @param format
     * @return
     */
    public static Date parseDateStr(String dateStr, String format) {
        SimpleDateFormat formatDate = new SimpleDateFormat(format);
        try {
            return formatDate.parse(dateStr);
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * parse date string to Date object.
     *
     * @param date
     * @param format
     * @return
     */
    public static Date getFormatDate(Date date, String format) {
        SimpleDateFormat formatDate = new SimpleDateFormat(format);
        try {
            return formatDate.parse(formatDate.format(date));
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * parse date string to Date object.
     *
     * @param dateStr
     * @return
     */
    public static Date parseDateStr(String dateStr) {
        Date date = new Date();
        String thisYear = getTimeByFromat(date, "yyyy年");
        SimpleDateFormat formatDate = new SimpleDateFormat(CN_TIME_FORMAT_SEPERATE);
        dateStr = thisYear + dateStr + ":00";
        try {
            return formatDate.parse(dateStr);
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * Translate time by the format.
     *
     * @param format
     * @return
     */
    public static String getTimeByFromat(Date date, String format) {
        if (date == null) {
            return "";
        } else {
            DateFormat df = new SimpleDateFormat(format);
            String time = df.format(date);
            return time;
        }
    }

    /**
     * 获取当天时间的0点0分0秒
     *
     * @return
     */
    public static Date getTodayStart() {
        String nowDate = DateUtils.getTimeByFromat(new Date(), "yyyy-MM-dd") + " 00:00:00";
        return DateUtils.parseDateStr(nowDate, "yyyy-MM-dd HH:mm:ss");
    }

    public static String stringToDate(String date) {
        SimpleDateFormat sdf = new SimpleDateFormat(HOUR_OF_MINUTE);
        String sd = sdf.format(new Date(Long.parseLong(date)));
        return sd;
    }

    public static String stringToDate(long date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        String sd = sdf.format(new Date(date));
        return sd;
    }

    public static Date add(Date date, int calendarField, int amount) {
        if (date == null) {
            throw new IllegalArgumentException("The date must not be null");
        }
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(calendarField, amount);

        return c.getTime();
    }

    /**
     * @param format 时间格式类型
     * @author chengfy
     * created at 2015/8/11 17:13
     * Description 获取系统当前时间
     */
    public static String getCurrentTime(String format) {
        DateFormat df = new SimpleDateFormat(format);
        String time = df.format(System.currentTimeMillis());
        return time;
    }

    /**
     * @param startMap 开始计时的时间
     * @return Map<String, Integer> key1: diffHours; key2: diffMinutes;key3: diffSeconds
     * @author chengfy
     * created at 2015/8/13 13:38
     * Description 倒计时
     */
    public static Map<String, Integer> countDownTime(Map<String, Integer> startMap) {
        Map<String, Integer> endMap = new HashMap<>();
        endMap.put("diffHours", 0);
        endMap.put("diffMinutes", 0);
        endMap.put("diffSeconds", 0);
        if (startMap == null)
            return startMap;
        int seconds = getSeconds(startMap);
        seconds--;
        endMap = getDiffMap(seconds);
        return endMap;
    }

    /**
     * @param time 单位秒
     * @return Map<String, Integer> key1: diffHours; key2: diffMinutes; key3: diffSeconds
     * @author chengfy
     * created at 2015/8/13 13:44
     * Description
     */
    public static Map<String, Integer> getDiffMap(int time) {
        Map<String, Integer> resultMap = new HashMap<>();
        if (time <= 0) {
            resultMap.put("diffHours", 0);
            resultMap.put("diffMinutes", 0);
            resultMap.put("diffSeconds", 0);
            return resultMap;
        }
        resultMap.put("diffHours", time / 3600);
        resultMap.put("diffMinutes", (time % 3600) / 60);
        resultMap.put("diffSeconds", (time % 3600) % 60);
        return resultMap;
    }

    /**
     * @author chengfy
     * created at 2015/8/13 14:01
     * Description timeMap转换成秒
     */
    public static int getSeconds(Map<String, Integer> timeMap) {
        int seconds = 0;
        seconds = timeMap.get("diffHours") * 3600 + timeMap.get("diffMinutes") * 60 + timeMap.get("diffSeconds");
        return seconds;
    }

    /**
     * @author gaozhilu
     * created at 16/4/25 下午2:21
     * @description 获取时间格式的图片名
     */
    public static String getTempName() {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHH:mm:ss");
        Date now = new Date(System.currentTimeMillis());
        String str = format.format(now);
        return (str.replace(":", "") + ".jpg");
    }

    public static Date getBeforeDay(Date date) {
        //获得前一天的日期时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        date = calendar.getTime();
        return date;
    }

    public static boolean isDateEqual(Date date1, Date date2) {
        //传入的两个时间是否相等
        String date1Str = DateUtils.getTimeByFromat(date1, "yyyy-MM-dd");
        String date2Str = DateUtils.getTimeByFromat(date2, "yyyy-MM-dd");
        return isDateEqual2(date1Str, date2Str);
    }

    public static boolean isDateEqual(String date1, Date date2) {
        //传入的两个时间是否相等
        String date2Str = DateUtils.getTimeByFromat(date2, "yyyy-MM-dd");
        return isDateEqual2(date1, date2Str);
    }

    public static boolean isDateEqual2(String str1, String str2) {
        boolean isEqual = false;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date dt1 = null;
        Date dt2 = null;
        try {
            dt1 = sdf.parse(str1);
            dt2 = sdf.parse(str2);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (dt1.getTime() == dt2.getTime()) {
            isEqual = true;
        } else {
            isEqual = false;
        }
        return isEqual;
    }

    public static boolean isDateOneBigger(Date date1, Date date2) {
        //传入的两个时间是否相等
        String date1Str = DateUtils.getTimeByFromat(date1, "yyyy-MM-dd");
        String date2Str = DateUtils.getTimeByFromat(date2, "yyyy-MM-dd");
        return isDateOneBigger2(date1Str, date2Str);
    }

    public static boolean isDateOneBigger2(String str1, String str2) {
        boolean isBigger = false;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date dt1 = null;
        Date dt2 = null;
        try {
            dt1 = sdf.parse(str1);
            dt2 = sdf.parse(str2);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (dt1.getTime() > dt2.getTime()) {
            isBigger = true;
        } else if (dt1.getTime() <= dt2.getTime()) {
            isBigger = false;
        }
        return isBigger;
    }

    //获取今天周几
    public static String getWeek() {
        Calendar cal = Calendar.getInstance();
        int i = cal.get(Calendar.DAY_OF_WEEK);
        switch (i) {
            case 1:
                return "星期日";
            case 2:
                return "星期一";
            case 3:
                return "星期二";
            case 4:
                return "星期三";
            case 5:
                return "星期四";
            case 6:
                return "星期五";
            case 7:
                return "星期六";
            default:
                return "";
        }
    }

    public static Date stringToDate(String strTime, String formatType)
            throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat(formatType);
        Date date = null;
        date = formatter.parse(strTime);
        return date;
    }
}
